<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <menu-search @search="reload"  />
    <el-card shadow="never">
     
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        :parse-data="parseData"
        @done="onDone"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            v-permission="['JS00']"
            @click="openEdit({type:1},1)"
          >
            添加
          </el-button>
          <el-button
            size="small"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            @click="reportexcel"
          >
            导出
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="expandAll">
            展开全部
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="foldAll">
            折叠全部
          </el-button>
        </template>
        <!-- 标题列 -->
        <template slot="title" slot-scope="{ row }">
          <i :class="row.icon"></i> {{ row.title }}
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" icon="el-icon-view"  @click="openEdit(row,'3')">详情</el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            v-role="['JS00']"
            @click="openEdit({parentId:row.id,parentTitle:row.title,type:1},'1')"
          >
            添加
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            v-role="['JS00']"
            @click="openEdit(row,'2')"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            v-role="['JS00']"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              v-role="['JS00']"
              icon="el-icon-delete"
            >
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <menu-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
      :btnindex="btnindex"
    />
    <!-- 导出数据 -->
    <ExportDialog ref="ExportDialog"></ExportDialog>
  </div>
</template>

<script>
  import MenuEdit from './components/menu-edit';
  import MenuSearch from './components/menu-search';
  import ExportDialog from '@/components/Excel/ExportDialog'
  import { AllMenuView,RemoveMenu } from '@/api/usermanager/menumanager';
  export default {
    name: 'SystemMenu',
    components: { MenuEdit,MenuSearch,ExportDialog },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'title',
            label: '菜单名称',
            showOverflowTooltip: true,
            minWidth: 100,
            slot: 'title',
          },
          {
            prop: 'path',
            label: '菜单地址',
            showOverflowTooltip: true,
            minWidth: 200,
          },
          {
            prop: 'orderIndex',
            label: '菜单顺序',
            showOverflowTooltip: true,
            minWidth: 60,
          },
          {
            prop: 'menuTypeDesc',
            label: '菜单类型',
            showOverflowTooltip: true,
            width: 80,
            align: 'center',
          },
          {
            prop: 'createDate',
            label: '创建时间',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center',
            formatter: (row, column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'createUserName',
            label: '创建人',
            showOverflowTooltip: true,
            minWidth: 80,
            align: 'center',
          },
          {
            prop: 'statusDesc',
            label: '状态',
            showOverflowTooltip: true,
            width: 80,
            align: 'center',
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            resizable: false,
            slot: 'action',
            hideInSetting: true,
            showOverflowTooltip: true
          }
        ],
        //操作按钮索引
        btnindex: '1',
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 全部菜单数据
        menuList: [],
        //是否展示操作按钮，只有admin才展示
        showOperation:false,
        // 导出列
        exportList: [
          { name: "菜单名称", keys: "title", status: true },
          { name: "菜单地址", keys: "path", status: true },
          { name: "菜单顺序", keys: "orderIndex", status: true },
          { name: "菜单类型", keys: "menuTypeDesc", status: true },
          { name: "创建时间", keys: "createDate", status: true },
          { name: "创建人", keys: "createUserName", status: true },
        ]
      };
    },
    computed:{
      
    },

    created(){
    },
    methods: {

      /* 表格数据源 */
      datasource(params) {
        let condition = {
          title:params.where.title,
          status:params.where.status,
        }
        return AllMenuView(condition);
      },

      /* 数据转为树形结构 */
      parseData(data) {
        return this.$util.toTreeData({
          data: data,
          idField: 'id',
          parentIdField: 'parentId'
        });
      },
      /* 表格渲染完成回调 */
      onDone({ data }) {
        // 编辑弹窗编辑完成后 done 事件回传表单数据，使用 Object.assign 赋值给 current 即可修改表格的行数据
        if(this.current) {
            Object.assign(this.current, data);
        }
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ where: where });
      },
      
      /**
       * 显示编辑
       * @param row 当前行对像
       */
      openEdit(row,index) {
        this.current = null;
        this.current = row;
        this.showEdit = true;
        this.btnindex = index;
      },
      /* 删除 */
      remove(row) {
        if (row.hasChild) {
          this.$message.error('请先删除子节点');
          return;
        }
        const loading = this.$loading({ lock: true });
        RemoveMenu(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 导出 */
      reportexcel(pams) {
        this.$refs.ExportDialog.showEdits = true
        this.$refs.ExportDialog.checkList = this.exportList
        this.$refs.ExportDialog.titleName = "菜单列表"
        this.$refs.ExportDialog.dataList = this.$refs.table.tableData;
      },
      /* 展开全部 */
      expandAll() {
        this.$refs.table.toggleRowExpansionAll(true);
      },
      /* 折叠全部 */
      foldAll() {
        this.$refs.table.toggleRowExpansionAll(false);
      },
    }
  };
</script>
